﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Modeling;
using devtm.Editor.TypeSelector;



namespace devtm.AutoMapper
{
    public partial class TypeElement
    {

        public Type Type
        {
            get
            {                

                Type t = MappingModel.FindType(AssemblyQualifiedName);

                return t;

            }
        }

        public void RefreshMappers(bool refresh)
        {

            if (NewMapperHelper.IsBusy)
                return;

            foreach (MappingDestination item in this.DMappings)
                if (item.Mapper != null && item.Mapper.MappingSource != null && item.Mapper.MappingSource.TypeElement != null && !string.IsNullOrEmpty(item.Mapper.MappingSource.TypeElement.AssemblyQualifiedName))
                    Mapper.UpdateProperties(item.Mapper.MappingSource, refresh);

            foreach (MappingSource item in this.SMappings)
                if (item.Mapper != null && item.Mapper.MappingSource != null && !string.IsNullOrEmpty(item.Mapper.MappingSource.TypeElement.AssemblyQualifiedName))
                    Mapper.UpdateProperties(item, refresh);

        }


        #region AssemblyQualifiedName

        private string _AssemblyQualifiedName;
        internal string GetAssemblyQualifiedNameValue()
        {
            return _AssemblyQualifiedName ?? string.Empty;
        }

        internal void SetAssemblyQualifiedNameValue(string newValue)
        {
            
            _AssemblyQualifiedName = newValue;

            if (!string.IsNullOrEmpty(newValue))
            {

                Type type = null;

                try
                {
                    type = this.MappingModel.FindType(newValue);
                }
                catch (Exception)
                {
                    return;
                }



                using (Transaction trans = Store.TransactionManager.BeginTransaction("Update Type"))
                {

                    if (type != null)
                    {
                        Namespace = type.Namespace;
                        FullName = type.FullName;
                        Name = type.Name;
                    }
                    else
                    {
                        Namespace = String.Empty;
                        FullName = String.Empty;
                        Name = String.Empty;
                    }

                    RefreshMappers(false);
                    trans.Commit();
                }
                return;
            }

            using (Transaction trans = Store.TransactionManager.BeginTransaction("Update Type"))
            {

                Namespace = String.Empty;
                FullName = String.Empty;
                Name = String.Empty;
                trans.Commit();
            }
        }

        #endregion



    }
}
